package com.example.toutiao.ui.main.presenter

import com.example.toutiao.http.HttpUtils
import com.example.toutiao.http.ResponseListener
import com.example.toutiao.http.UserApi
import com.example.toutiao.mvp.model.BaseModel
import com.example.toutiao.mvp.presenter.BasePresenter
import com.example.toutiao.ui.main.model.RecommenModel
import com.example.toutiao.ui.main.model.TabIdsModel
import com.example.toutiao.ui.main.model.TabListModel
import com.example.toutiao.ui.main.view.HomeView

class HomePresenter: BasePresenter<HomeView>() {
    fun getTabIds(){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getTabIds(),object :
            ResponseListener<BaseModel<List<TabIdsModel>>> {
            override fun onSuccess(data: BaseModel<List<TabIdsModel>>) {
                if(data!=null&&data.code==200){
                    getBaseView()!!.setData(data.data)
                }
            }
            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }
        })
    }
    fun getRecommend(){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getRecommend(),object :
            ResponseListener<BaseModel<List<RecommenModel>>> {
            override fun onSuccess(data: BaseModel<List<RecommenModel>>) {
                if(data!=null&&data.code==200){
                    getBaseView()!!.setRecommend(data.data as MutableList<RecommenModel>)
                }
            }
            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }
        })
    }
    fun getChannel(){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getChannel(),object :
            ResponseListener<BaseModel<TabListModel>> {
            override fun onSuccess(data: BaseModel<TabListModel>) {
                if(data!=null&&data.code==200){
                    getBaseView()!!.setChannel(data.data as TabListModel)
                }
            }
            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }
        })
    }

}